package com.jimet.user;

import java.sql.ResultSet;
import java.util.Properties;
import java.util.Vector;
import com.jimet.db.JimetUserDB;
import com.jimet.group.*;


public class JimetUsers {


	protected int pos;
	public Vector<JimetUser> userVec;
	Properties dbFieldProp;
	
	public JimetUsers(){
		dbFieldProp = new Properties();
		dbFieldProp.put("id","USR_ID");
		dbFieldProp.put("name","USR_USERNAME");
		dbFieldProp.put("createDate","USR_CREATE_DATE");
	}

	
	public int size()
	{
		if(this.userVec == null)
			return 0;
		else
			return this.userVec.size();
	}
	
	public void add(JimetUser juser)
	{

		if(this.userVec == null)
		{	
			this.pos = -1;
			this.userVec = new Vector<JimetUser>();
		}
		this.userVec.add(juser);
	}
	
	public void store() throws Exception
	{
		for(int i=0;i<userVec.size();i++)
		{
			JimetUser juser = (JimetUser) userVec.elementAt(i);
			juser.store();
		}
	}
	
	public void delete() throws Exception
	{
		for(int i=0;i<userVec.size();i++)
		{
			JimetUser juser = (JimetUser) userVec.elementAt(i);
			juser.delete();
		}
	}
	
	public void load(String strSort, String strOrderBy) throws Exception
	{ 
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getAllUserSet(strSort,getDBFieldName(strOrderBy));
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			this.userVec.add(juser);

		}
		
		jimdb.disconnect();
	}
	public void load() throws Exception
	{ 
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getAllUserSet();
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			this.userVec.add(juser);

		}
		
		jimdb.disconnect();
	}	
	public void loadByGroup(int groupId) throws Exception
	{ 
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getUsersByGroup(groupId);
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			this.userVec.add(juser);

		}
		
		jimdb.disconnect();
	}
	public void loadByAuthoriztion( String authName ) throws Exception
	{ 
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getUsersByAuth(authName);
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			this.userVec.add(juser);
		}
		
		jimdb.disconnect();
	}	
	  public void search(String strSort, String strLike, String strSortBy,String strSearchBy) throws Exception
	{
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getUserSearchResults(strSort,strLike,strSortBy,strSearchBy);
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			this.userVec.add(juser);

		}
		
		jimdb.disconnect();
	}
	

	public void loadByIds (String [] strArrUserIDs,String strSort, String strOrderBy) throws Exception
	{
		this.loadByUserIDs(strArrUserIDs, strSort, strOrderBy);
	}
	public void loadByUserIDs(String [] strArrUserIDs,String strSort, String strOrderBy) throws Exception
	{
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getUsersByID(strArrUserIDs,strSort,getDBFieldName(strOrderBy));
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.setUserId(rs.getInt(1));
			juser.setUsername(rs.getString(2));
			juser.setPasswordMD5(rs.getString(3));
			juser.setCreateDate(rs.getTimestamp(4));
			juser.setTimeStamped(rs.getBoolean(5));
			juser.setTimeStampID(rs.getInt(6));
			juser.setUsernameAlt(rs.getString(7));
			this.userVec.add(juser);
		}
		
		jimdb.disconnect();
	}
	
	public void loadByUserName(String userName,String strSort, String strOrderBy) throws Exception
	{
		this.pos = -1;
		this.userVec = new Vector<JimetUser>();
		JimetUserDB jimdb = new JimetUserDB();		
		ResultSet rs = jimdb.getUserByUserName(userName,getDBFieldName(strOrderBy),strSort);
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.setUserId(rs.getInt(1));
			juser.setUsername(rs.getString(2));
			juser.setPasswordMD5(rs.getString(3));
			juser.setCreateDate(rs.getTimestamp(4));
			juser.setTimeStamped(rs.getBoolean(5));
			juser.setTimeStampID(rs.getInt(6));
			juser.setUsernameAlt(rs.getString(7));
			this.userVec.add(juser);
		}
		
		jimdb.disconnect();
	}
	
	public void setGroupIds(String groupId) throws Exception
	{
		while(this.next()) {
			JimetUser juser = this.getNext();
			
			while(juser.getUserGroups().next()) {
				JimetGroup jgroup = juser.getUserGroups().getNext();
				jgroup.setID(Integer.parseInt(groupId));
				
			}
			
		}
	}
	
	public boolean contains(JimetUser lookFor)
	{
		if(this.userVec != null)
		{
			for(int i=0;i<this.userVec.size();i++)
			{
				JimetUser anObject = (JimetUser) this.userVec.elementAt(i);
				if(anObject.getUserId() == lookFor.getUserId())
					return true;
			}
		}
		return false;
	}

	public void reset()
	{
		this.pos = -1;
	}

	public boolean next()
	{
		if(this.userVec != null) {
		pos++;
		return  (pos < this.userVec.size());
		} else {
			return false;
		}
	}

	public JimetUser getNext()
	{	
		return (JimetUser) this.userVec.elementAt(this.pos);
	}
	private String getDBFieldName(String str)
	{
		return this.dbFieldProp.getProperty(str);
	
	}	
}
